﻿namespace Snipps.Validators
{
    using System;

    using FluentValidation;

    using Snipps.Core.Entities;
    using Snipps.Langs;
    using Snipps.ViewModels.Pages;

    public class EditSnippetValidator : AbstractValidator<EditSnippetViewModel>
    {
        public EditSnippetValidator()
        {
            BaseRules();
        }

        public EditSnippetValidator(Func<string, ProgrammingLang, bool> isSnippetExists)
        {
            BaseRules();

            RuleFor(x => x.ValidationSummary).Must((m, p) => !isSnippetExists(m.SnippetName, m.ProgrammingLangFilter.CurrentProgrammingLang)).WithMessage(Lang.ValidationSnippetExists);
        }

        private void BaseRules()
        {
            RuleFor(x => x.SnippetName).Length(4, 255).WithMessage(Lang.ValidationSnippetNeedMin4Char);
            RuleFor(x => x.SnippetContent).NotEmpty().WithMessage(Lang.ValidationSnippetContentEmpty);
        }
    }
}